#-*- coding:utf-8 –*-
from urllib import request
from urllib import error
from bs4 import BeautifulSoup
import os
import time
import json

HOST = 'https://sg.zuiyouxi.com/gamecards'
IMG_HOST = 'https://static.zuiyouxi.com/web/img'
DATA_HOST = 'https://sg.zuiyouxi.com/gamecard/wj'

CIDS = [
    {'key': 4, 'value': u'魏国'},
    {'key': 5, 'value': u'蜀国'},
    {'key': 6, 'value': u'吴国'},
    {'key': 7, 'value': u'吴国'},
]

STARS = [
    {'key': 3, 'value': u'3星'},
    {'key': 4, 'value': u'4星'},
    {'key': 5, 'value': u'5星'},
]

class CardParser:
    def __init__(self, data):
        self._html = BeautifulSoup(data, 'lxml')
        self.updateData()

    def fillKeyValue(self, child, tag):
        if child.name != tag:
            return
        strs = child.text.split("：")
        if len(strs) == 2:                
            self._datas.append({'key':strs[0].strip(), 'value':strs[1].strip()})        

    def updateP(self, child):
        if child.name != 'p':
            return
        for item in child.contents:
            self.fillKeyValue(item, 'span')

    def updateDiv(self, child):
        if child.name != 'div':
            return  
        id = child.attrs['id'] 
        if id == 'guo-jian':
            for item in child.contents:
                self.updateDiv(item)
        elif id == 'guo-ji':

        elif id == 'guo-zi':
            for item in child.contents:
                self.fillKeyValue(item, 'h5')                
                self.updateP(item)
        elif id == 'guo-jie':
            key = child.h6.text
            value = child.div.p.text
            self._datas.append({'key':key.strip(), 'value':value.strip()})

    def updateData(self):
        self._datas = []
        for child in self._html.div.contents:
            self.updateDiv(child)                        

def createCard(card, path):
    id = card['id']
    picUrl = '%s/%s'%(IMG_HOST, card['pic'])
    picPath = os.path.join(path, 'card_%s.png'%id)
    dataUrl = '%s?id=%s'%(DATA_HOST, id)
    if os.path.isfile(picPath):
        try:                
            with request.urlopen(picUrl) as f:
                data = f.read()
                fw = open(picPath, 'wb')
                fw.write(data)
                fw.close()

                print(card['name'])

            with request.urlopen(dataUrl) as f:
                data = f.read()
                data = data.decode('utf-8')
                data = data.replace('\n', '')
                data = data.replace('\r', '')                
                CardParser(data)                

        except error.HTTPError as e:                     
            time.sleep(2)
        except error.URLError as e:
            print(e)         

def createCards(data, path):
    obj = json.loads(data, strict=False)
    cards = obj['cards']['cards']
    for card in cards:
        createCard(card, path) 

def createStar(star, folder, country):
    path = os.path.join(folder, star['value'])
    if not os.path.isdir(path):
        os.makedirs(path)
    key = star['key']

    pageIndex = 1
    while True:
        try:        
            url = '%s?cid=%d&stars=%d&page=%d'%(HOST, country, key, pageIndex)
            with request.urlopen(url) as f:
                data = f.read()
                data = data.decode('unicode_escape')                
                createCards(data, path)
            pageIndex = pageIndex + 1
        except error.HTTPError as e:                     
            time.sleep(2)
        except error.URLError as e:
            print(e)
            break 

def createCountry(cid):    
    path = os.path.join('.', cid['value'])
    if not os.path.isdir(path):
        os.makedirs(path)    
    key = cid['key']
    for star in STARS:
        createStar(star, path, key)

if __name__ == "__main__":
    for cid in CIDS:
        createCountry(cid)    